var Report = {
    data: {},
    dataVersion: '',
    serialBind: '',
    serialRead: '',
    hideToast: '',
    fingerSensorIsNull: false,
    sendMessage: function () {
        try {
            zz_send(JSON.stringify({event: 402, data: {paycheckresult: '专业验机数据'}}));
        } catch (e) {

        }
    },
    init: function () {
        window['cr'] = {
            receiveMessage: function (e) {
                e = e.replace(/\"|\u0000|\u0001|\u0002|\u0003|\u0004|\u0005|\u0006|\u0007|\u0008|\u0009|\u000a|\u000b|\u000c|\u000d|\u000e|\u000f|\u0010|\u0011|\u0012|\u0013|\u0014|\u0015|\u0016|\u0017|\u0018|\u0019|\u001a|\u001b|\u001c|\u001d|\u001e|\u001f|\u007F/g, '');
                var msg = JSON.parse(e);
                console.log('接收pc返回的验机报告', msg);
                //电池电量
                if (msg.event === 207) {
                    $('#batLife').text(msg.data.battery + '%');
                    if (msg.data.battery * 1 < 80) {
                        $('#batLife').css('color', 'rgba(255, 0, 24, 1)');
                    }
                }
                //整机序列号显隐
                if (msg.event === 203) {
                    Report.hideSerial();
                    clearTimeout(Report.hideToast);
                    $('#toast').css('opacity', '0');
                }
                //强制隐藏截图提示
                if (msg.event === 250) {
                    clearTimeout(Report.hideToast);
                    $('#toast').css('opacity', '0');
                }
                //截图提示
                if (msg.event === 260) {
                    $('#toast').css('opacity', '1');
                    Report.hideToast = setTimeout(function () {
                        $('#toast').css('opacity', '0');
                    }, 5 * 1000);
                }
                if (msg.event === 400) {
                    if (msg.data.paycheckresult) {
                        Report.serialBind = '';
                        Report.serialRead = '';
                        Report.data = msg.data.paycheckresult.data;
                        Report.initHeader();
                        Report.initLeftDeviceInfo();
                        Report.initTableData();
                        var msgData = {paycheckresult: '页面加载完成'};
                        if (Report.fingerSensorIsNull) {
                            Object.defineProperty(msgData, 'offset', {
                                value: 58,
                                writable: true,
                                enumerable: true,
                                configurable: true
                            });
                        }
                        zz_send(JSON.stringify({
                            event: 401,
                            data: msgData
                        }));
                    } else if (msg.data.token) {
                        $.ajax({
                            url: i4BaseUrl + '/majorReport.xhtml',
                            data: decodeURIComponent(BASE64.encoder(JSON.stringify({
                                token: msg.data.token,
                                client: 1
                            }))),
                            headers: {json: msg.data.json || ''},
                            timeout: 10000,
                            type: 'post',
                            xhrFields: {
                                withCredentials: true // 允许跨域请求时携带COOKIE
                            },
                            dataType: 'json',
                            contentType: 'application/raw',
                            success: function (res) {
                                if (res.code == 0) {
                                    Report.serialBind = '';
                                    Report.serialRead = '';
                                    Report.data = res.data.reportData.data;
                                    Report.dataVersion = res.data.dataVersion;
                                    Report.initHeader();
                                    Report.initLeftDeviceInfo();
                                    Report.initTableData();
                                    var msgData = {paycheckresult: '页面加载完成'};
                                    if (Report.fingerSensorIsNull) {
                                        Object.defineProperty(msgData, 'offset', {
                                            value: 58,
                                            writable: true,
                                            enumerable: true,
                                            configurable: true
                                        });
                                    }
                                    zz_send(JSON.stringify({
                                        event: 401,
                                        data: msgData
                                    }));
                                }
                            },
                            error: function (errorObj) {
                            }
                        });
                    }
                }
            }
        };
        try {
            zz_send(JSON.stringify({event: 402, data: {paycheckresult: '专业验机数据'}}));
        } catch (e) {

        }
        window._pcReady = function () {
            Report.sendMessage();
        };
    },
    initWeb: function () {
        if (window.location.search) {
            var arr = window.location.href.split('?').pop().split('#').shift().split('&');
            var token = '';
            for (var i = arr.length - 1; i >= 0; i--) {
                if (arr[i].match('token=')) {
                    token = arr[i].split('token=')[1];
                }
            }
            $.ajax({
                url: 'https://test-app4.i4.cn/majorReport/getByToken.go',
                data: {token: token},
                headers: {},
                timeout: 10000,
                type: 'get',
                xhrFields: {
                    withCredentials: true // 允许跨域请求时携带COOKIE
                },
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        Report.serialBind = '';
                        Report.serialRead = '';
                        Report.data = res.data.reportData.data;
                        Report.initHeader();
                        Report.initLeftDeviceInfo();
                        Report.initTableData();
                        var msgData = {paycheckresult: '页面加载完成'};
                        if (Report.fingerSensorIsNull) {
                            Object.defineProperty(msgData, 'offset', {
                                value: 58,
                                writable: true,
                                enumerable: true,
                                configurable: true
                            });
                        }
                        zz_send(JSON.stringify({
                            event: 401,
                            data: msgData
                        }));
                    }
                },
                error: function (errorObj) {
                    console.log(errorObj);
                }
            });
        }
    },
    //初始化头部
    initHeader: function () {
        //背景 touchIdCheckColor 字段 ： 0 红 1 绿 2 黄
        if (Report.data.touchIdCheckColor == 0) {
            // 异常
            Report.setText('.remarkInfo', Report.data.touchIdCheck, 'rgba(255, 0, 24, 1)');
            $('#headerBg').css('background', 'url(img/report/header/exception.svg)');
            //中部左右边框
            $('#headerBg .remark')
                .css('border-left', '1px solid rgba(255, 77, 79, .2)')
                .css('border-right', '1px solid rgba(255, 77, 79, .2)');// 异常
        } else if (Report.data.touchIdCheckColor == 1) {
            // 正常
            Report.setHeaderImg('.remarkInfo', Report.data.touchIdCheck, 'rgba(51, 176, 113, 1)');
            $('#headerBg').css('background', 'url(img/report/header/normal.svg)');
            //中部左右边框
            $('#headerBg .remark')
                .css('border-left', '1px solid rgba(51, 176, 113, .2)')
                .css('border-right', '1px solid rgba(51, 176, 113, .2)');
        } else if (Report.data.touchIdCheckColor == 2) {
            // 警告
            Report.setHeaderImg('.remarkInfo', Report.data.touchIdCheck, 'rgba(255, 128, 16, 1)');
            $('#headerBg').css('background', 'url(img/report/header/warning.svg)');
            //中部左右边框
            $('#headerBg .remark')
                .css('border-left', '1px solid rgba(255, 170, 0, 1)')
                .css('border-right', '1px solid rgba(255, 170, 0, 1)');
        }
        //验机时间
        $('.remarkTime').text(Report.data.checkDate);
        if (Report.data.sumUpCheck) {
            for (var i = 0; i < Report.data.sumUpCheck.length; i++) {
                var color = Report.colorChangeToRgba(Report.data.sumUpCheck[i].color);
                if (color == 'rgba(51, 176, 113, 1)') {
                    color = 'rgba(72, 85, 102, 1)';
                }
                Report.setHeaderImg('#infoImg' + Report.data.sumUpCheck[i].key, Report.data.sumUpCheck[i].value, Report.data.sumUpCheck[i].color, color, Report.data.sumUpCheck[i].key);
            }
        }
    },
    //初始化左侧设备信息
    initLeftDeviceInfo: function () {
        if (Report.data.orderItem) {
            for (var i = 0; i < Report.data.orderItem.length; i++) {
                Report.setText('#' + Report.data.orderItem[i].key, Report.data.orderItem[i].value, Report.colorChangeToRgba(Report.data.orderItem[i].color));
                if (Report.data.orderItem[i].key == 'batLife') {
                    $('#batLife').text(Report.data.orderItem[i].value + '%');
                    if (Report.data.orderItem[i].value * 1 < 80) {
                        $('#batLife').css('color', 'rgba(255, 0, 24, 1)');
                    }
                }
            }
            for (var i = 0; i < Report.data.orderItem.length; i++) {
                if (Report.data.orderItem[i].key == 'isBS' && Report.dataVersion == '1') {
                    Report.setText('#deviceType', Report.data.orderItem[i].value, Report.colorChangeToRgba(Report.data.orderItem[i].color));
                    $('#isBSName').text('是否BS机');
                }
            }
            try {
                if (Report.data.isSupervised) {
                    $('#isSupervised').text(Report.data.brand + '出租机').css('display', 'inline-block');
                }
            } catch (e) {

            }
        }
    },
    //初始化表格信息
    initTableData: function () {
        try {
            //设备型号
            Report.setText('#deviceModelStatus', Report.data.key_model.result, Report.colorChangeToRgba(Report.data.key_model.status));
            Report.rowDisplay('#deviceModelBind', Report.data.key_model.bin);
            Report.rowDisplay('#deviceModelRead', Report.data.key_model.read);
            Report.rowDisplay('#deviceModelSearch', Report.data.key_model.search);
            //设备颜色
            Report.setText('#deviceColorStatus', Report.data.key_color.result, Report.colorChangeToRgba(Report.data.key_color.status));
            Report.rowDisplay('#deviceColorBind', Report.data.key_color.bin);
            Report.rowDisplay('#deviceColorRead', Report.data.key_color.read);
            Report.rowDisplay('#deviceColorSearch', Report.data.key_color.search);
            //磁盘容量
            Report.setText('#disksizeStatus', Report.data.key_disksize.result, Report.colorChangeToRgba(Report.data.key_disksize.status));
            Report.rowDisplay('#disksizeBind', Report.data.key_disksize.bin);
            Report.rowDisplay('#disksizeRead', Report.data.key_disksize.read);
            Report.rowDisplay('#disksizeSearch', Report.data.key_disksize.search);
            //销售型号
            Report.setText('#ModleNumberStatus', Report.data.key_ModleNumber.result, Report.colorChangeToRgba(Report.data.key_ModleNumber.status));
            Report.rowDisplay('#ModleNumberBind', Report.data.key_ModleNumber.bin);
            Report.rowDisplay('#ModleNumberRead', Report.data.key_ModleNumber.read);
            Report.rowDisplay('#ModleNumberSearch', Report.data.key_ModleNumber.search);
            if (Report.data.key_ModleNumber.deviceType) {
                Report.setText('#ModleNumberIsShowDevice', Report.data.key_ModleNumber.deviceType.data, Report.colorChangeToRgba(Report.data.key_ModleNumber.deviceType.color));
            }
            //销售地区
            Report.setText('#regionInfoStatus', Report.data.key_regionInfo.result, Report.colorChangeToRgba(Report.data.key_regionInfo.status));
            Report.rowDisplay('#regionInfoBind', Report.data.key_regionInfo.bin);
            Report.rowDisplay('#regionInfoRead', Report.data.key_regionInfo.read);
            Report.rowDisplay('#regionInfoSearch', Report.data.key_regionInfo.search);
            //监管型号
            Report.setText('#regulatoryModelStatus', Report.data.key_regulatoryModel.result, Report.colorChangeToRgba(Report.data.key_regulatoryModel.status));
            Report.rowDisplay('#regulatoryModelBind', Report.data.key_regulatoryModel.bin);
            Report.rowDisplay('#regulatoryModelRead', Report.data.key_regulatoryModel.read);
            Report.rowDisplay('#regulatoryModelSearch', Report.data.key_regulatoryModel.search);
            //蓝牙地址
            Report.setText('#bluetAddressStatus', Report.data.key_bluetAddress.result, Report.colorChangeToRgba(Report.data.key_bluetAddress.status));
            Report.rowDisplay('#bluetAddressBind', Report.data.key_bluetAddress.bin);
            Report.rowDisplay('#bluetAddressRead', Report.data.key_bluetAddress.read);
            //蜂窝地址
            Report.setText('#etherAddressStatus', Report.data.key_etherAddress.result, Report.colorChangeToRgba(Report.data.key_etherAddress.status));
            Report.rowDisplay('#etherAddressBind', Report.data.key_etherAddress.bin);
            Report.rowDisplay('#etherAddressRead', Report.data.key_etherAddress.read);
            //WiFi地址
            Report.setText('#wifiStatus', Report.data.key_wifiAddress.result, Report.colorChangeToRgba(Report.data.key_wifiAddress.status));
            Report.rowDisplay('#wifiBind', Report.data.key_wifiAddress.bin);
            Report.rowDisplay('#wifiRead', Report.data.key_wifiAddress.read);
            //整机序列号
            if (Report.data.key_serial.result) {
                $('#serial').css('display', 'inline-block');
            }
            Report.setText('#serialStatus', Report.data.key_serial.result, Report.colorChangeToRgba(Report.data.key_serial.status));
            Report.rowDisplay('#serialBind', Report.data.key_serial.bin);
            Report.rowDisplay('#serialRead', Report.data.key_serial.read);
            Report.rowDisplay('#serialBindData', Report.getRemarkText(Report.data.key_serial.binRegn, Report.data.key_serial.bindate));
            Report.rowDisplay('#serialReadData', Report.getRemarkText(Report.data.key_serial.readRegn, Report.data.key_serial.readdate));
            Report.hideSerial();
            //主板序列号
            if (Report.data.key_mlbSerial.result) {
                $('#mlbSerial').css('display', 'inline-block');
            }
            Report.setText('#mlbSerialStatus', Report.data.key_mlbSerial.result, Report.colorChangeToRgba(Report.data.key_mlbSerial.status));
            Report.rowDisplay('#mlbSerialBind', Report.data.key_mlbSerial.bin);
            Report.rowDisplay('#mlbSerialRead', Report.data.key_mlbSerial.read);
            Report.rowDisplay('#mlbSerialBindData', Report.getRemarkText(Report.data.key_mlbSerial.binRegn, Report.data.key_mlbSerial.bindate));
            Report.rowDisplay('#mlbSerialReadData', Report.getRemarkText(Report.data.key_mlbSerial.readRegn, Report.data.key_mlbSerial.readdate));
            //前置摄像头
            if (Report.data.key_fcms.result) {
                $('#fcms').css('display', 'inline-block');
            }
            Report.setText('#fcmsStatus', Report.data.key_fcms.result, Report.colorChangeToRgba(Report.data.key_fcms.status));
            Report.rowDisplay('#fcmsBind', Report.data.key_fcms.bin);
            Report.rowDisplay('#fcmsRead', Report.data.key_fcms.read);
            Report.rowDisplay('#fcmsBindData', Report.getRemarkText(Report.data.key_fcms.binRegn, Report.data.key_fcms.bindate));
            Report.rowDisplay('#fcmsReadData', Report.getRemarkText(Report.data.key_fcms.readRegn, Report.data.key_fcms.readdate));
            //后置摄像头
            if (Report.data.key_bcms.result) {
                $('#bcms').css('display', 'inline-block');
            }
            Report.setText('#bcmsStatus', Report.data.key_bcms.result, Report.colorChangeToRgba(Report.data.key_bcms.status));
            Report.rowDisplay('#bcmsBind', Report.data.key_bcms.bin);
            Report.rowDisplay('#bcmsRead', Report.data.key_bcms.read);
            Report.rowDisplay('#bcmsBindData', Report.getRemarkText(Report.data.key_bcms.binRegn, Report.data.key_bcms.bindate));
            Report.rowDisplay('#bcmsReadData', Report.getRemarkText(Report.data.key_bcms.readRegn, Report.data.key_bcms.readdate));
            //电池序列号
            if (Report.data.key_battery.result) {
                $('#batterySerialNo').css('display', 'inline-block');
            }
            Report.setText('#batterySerialNoStatus', Report.data.key_battery.result, Report.colorChangeToRgba(Report.data.key_battery.status));
            Report.rowDisplay('#batterySerialNoBind', Report.data.key_battery.bin);
            Report.rowDisplay('#batterySerialNoRead', Report.data.key_battery.read);
            Report.rowDisplay('#batterySerialNoBindData', Report.getRemarkText(Report.data.key_battery.binRegn, Report.data.key_battery.bindate));
            Report.rowDisplay('#batterySerialNoReadData', Report.getRemarkText(Report.data.key_battery.readRegn, Report.data.key_battery.readdate));
            //液晶显示屏
            if (Report.data.key_lcm.result) {
                $('#lcm').css('display', 'inline-block');
            }
            Report.setText('#lcmStatus', Report.data.key_lcm.result, Report.colorChangeToRgba(Report.data.key_lcm.status));
            if (Report.data.key_lcm.status != 1) {
                $('#lcmStatus').css('display', 'inline-block');
            } else {
                if (Report.data.key_lcm.result == '需用户判断') {
                    $('#lcmLink').css('display', 'inline-block');
                } else {
                    Report.setText('#lcmInfo', Report.data.key_lcm.result, Report.colorChangeToRgba(Report.data.key_lcm.status));
                    $('#lcmInfo').css('display', 'inline-block');
                }
            }
            Report.rowDisplay('#lcmBind', Report.data.key_lcm.bin);
            Report.rowDisplay('#lcmRead', Report.data.key_lcm.read);
            Report.rowDisplay('#lcmBindData', Report.getRemarkText(Report.data.key_lcm.binRegn, Report.data.key_lcm.bindate));
            Report.rowDisplay('#lcmReadData', Report.getRemarkText(Report.data.key_lcm.readRegn, Report.data.key_lcm.readdate));
            //指纹
            if (Report.data.key_fingerSensor) {
                $('#fingerSensor').css('display', 'inline-block');
                Report.setText('#fingerSensorStatus', Report.data.key_fingerSensor.result, Report.colorChangeToRgba(Report.data.key_fingerSensor.status));
                Report.rowDisplay('#fingerSensorBind', Report.data.key_fingerSensor.bin);
                Report.rowDisplay('#fingerSensorRead', Report.data.key_fingerSensor.read);
                Report.rowDisplay('#fingerSensorBindData', Report.getRemarkText(Report.data.key_fingerSensor.binRegn, Report.data.key_fingerSensor.bindate));
                Report.rowDisplay('#fingerSensorReadData', Report.getRemarkText(Report.data.key_fingerSensor.readRegn, Report.data.key_fingerSensor.readdate));
            } else {
                Report.fingerSensorIsNull = true;
            }
        } catch (e) {
        }
    },
    //获取描述文本
    getRemarkText: function (text1, text2) {
        var a = text1 || '';
        var b = text2 || '';
        return a + ' ' + b;
    },
    //设置文字和颜色
    setText: function (selector, text, color) {
        $(selector).text(text);
        $(selector).attr('title', text);
        $(selector).css('color', color);
    },
    //将color字段转换为rgba颜色
    colorChangeToRgba: function (color) {
        var rgba = 'rgba(72, 85, 102, 1)';
        color = color * 1;
        switch (color) {
            case 1:
                rgba = 'rgba(51, 176, 113, 1)';
                break;
            case 2:
                rgba = 'rgba(142, 154, 169, 1)';
                break;
            case 3:
                rgba = 'rgba(255, 0, 24, 1)';
                break;
            case 4:
                rgba = 'rgba(255, 128, 16, 1)';
                break;
        }
        return rgba;
    },
    //设置头部颜色和图片
    setHeaderImg: function (selector, text, status, color, img) {
        var imgStatus = 'Warning';
        switch (status) {
            case 3:
                imgStatus = 'Exception';
                break;
            case 1:
                imgStatus = 'Normal';
                break;
            case 4:
                imgStatus = 'Warning';
                break;
        }
        $(selector).text(text).css('color', color);
        if (img) {
            $(selector).css('background', 'url(img/report/header/' + img + imgStatus + '.svg)');
        }
    },
    hideSerial: function () {
        if (!Report.serialBind) {
            Report.serialBind = $('#serialBind').text();
            Report.serialRead = $('#serialRead').text();
        }
        if ($('#serialRead').text().match(/\*/)) {
            $('#serialBind').text(Report.serialBind);
            $('#serialRead').text(Report.serialRead);
        } else {
            $('#serialBind').text($('#serialBind').text().substr(0, 3) + '*****' + $('#serialBind').text().substr(-4, 4));
            $('#serialRead').text($('#serialRead').text().substr(0, 3) + '*****' + $('#serialRead').text().substr(-4, 4));
        }
    },
    rowDisplay: function (selector, text) {
        $(selector).text(text).attr('title', text);
        if (selector.match(/Search/)) {
            if (text.length == 0) {
                $(selector).text(text).parent().css('display', 'none');
            }
        }
    }
};
